package com.kaluli.project.utils.nanshaxml;
import java.sql.Timestamp;
/**
 * 此类用于组装返利网Xml的推送报文
 * 此类的工作环境是多线程
 * @author zyzowen1944
 *  Date 2016-2-2
 */
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.http.client.utils.DateUtils;
import org.dom4j.*;
import org.dom4j.io.*;
public  class SelXmlBuilder {
	private static SelXmlBuilder xmlBuilder=null;
	private static Document document = null;
	private final static String ROOT="request";
	private final static String HEAD = "head";
	
	public final static String METHOD_TYPE = "methodType";
	public final static String BUS_CODE = "busCode";
	public final static String RULE = "rule";
	
	private final static String BODY = "body";
	public final static String ORDERNO = "orderNo";
	public final static String STARTTIME = "startTime";
	public final static String ENDTIME = "endTime";//OTO店铺代码
	public final static String HGAREA = "hgArea";//海关关区
	public final static String PAGE = "page";
	public final static String PAGESIZE = "pageSize";
	
	
	public  static SelXmlBuilder getInstance(){
		synchronized(SelXmlBuilder.class){  //避免多线程调用该方法时串行执行，效率低下
			//if(xmlBuilder==null){
				xmlBuilder =  new SelXmlBuilder();
				
			//}
		}
		return xmlBuilder;
	}
	
	public SelXmlBuilder(){
		document = DocumentFactory.getInstance().createDocument();
		document.addElement(ROOT);
		Element root = document.getRootElement();
		root.addElement(HEAD);
		root.addElement(BODY);
	}
	
	
	public void addHeader(String key,String value){
		String xpath = "//"+HEAD;
		@SuppressWarnings("unchecked")
		List<Element> composites = document.selectNodes(xpath);
		Element head = composites.get(0);
		head.addElement(key).addText(value);
	}
	
	public void addBody(String key,Object value,boolean isleaf,String parentName){
		String xpath = "/"+parentName;
		@SuppressWarnings("unchecked")
		List<Element> composites = document.selectNodes(xpath);
		Element body = composites.get(composites.size()-1);
		if(isleaf){ //如果是叶子节点，同时创建Tag和Value
			if(value.getClass().getName().equals(String.class.getName())){
				body.addElement(key).addText(value.toString());
			}else if(value.getClass().getName().equals(Date.class.getName()) || value.getClass().getName().equals(Timestamp.class.getName())){
				String date = DateUtils.formatDate((Date) value, "yyyyMMddHHmmss");
				body.addElement(key).addText(date);
			}else{
				body.addElement(key).addText(value.toString());
			}
			
		}else{  //如果是子节点
			body.addElement(key);
		}
	}
	
	public String toString(){
		return document.asXML();
	}
	public static void main(String s[]){
		/*EDIXmlBuilder xml = EDIXmlBuilder.getInstance("HG");
		xml.addHeader("MessageID", "CUST000000012312313");
		xml.addBody("swbebtrade", "", false, ROOT+"/"+BODY);
		xml.addBody("Record","",false,ROOT+"/"+BODY+"/swbebtrade");
		xml.addBody("EntInsideNo", "test0001", true, ROOT+"/"+BODY+"/swbebtrade/Record");//增加叶子 
		xml.addBody("swbetradeg",null, false	, ROOT+"/"+BODY+"/swbebtrade/Record");  //增加节点 
		xml.addBody("Record", "", false	, ROOT+"/"+BODY+"/swbebtrade/Record/swbetradeg");  //增加节点 
		xml.addBody("EntGoodsNo", "1", true, ROOT+"/"+BODY+"/swbebtrade/Record/swbetradeg/Record");
		xml.addBody("Record", "", false	, ROOT+"/"+BODY+"/swbebtrade/Record/swbetradeg");  //增加节点 
		xml.addBody("EntGoodsNo", "2", true, ROOT+"/"+BODY+"/swbebtrade/Record/swbetradeg/Record");
		//xml.addBody("", value, isleaf, parentName)
		
		//xml.addBody("EntGoodsNo", "1", true, parentName)
		System.out.println(xml.toString());*/
		SimpleDateFormat sFormat = new SimpleDateFormat("YYYYMMddHHmmss");
		String s2 = sFormat.format(new Date());
		System.out.print(s2);
	}
	
}
